package org.jivesoftware.spark.crowd.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jivesoftware.spark.crowd.Param;

public class IandAPanel extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 159L;
	private String title ;
	private JPanel messagePanel;
	private JPanel buttonPanel;
	private JLabel label;
	private String message;
	private String crowdName;
	private String ns;
	
	public IandAPanel(){
		
		
			
			message = "test";
		
			messagePanel = new JPanel();
			messagePanel.setOpaque(false);
			buttonPanel = new JPanel();
			buttonPanel.setOpaque(false);
		setLayout(new BorderLayout());
		String title = message;
		label = new JLabel(title);
		
		messagePanel.add(new JLabel(message));
		final JLabel accept = new JLabel("同意");
		accept.setForeground(new Color(0,215,0));
		accept.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
		accept.setOpaque(false);
		accept.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent e) {
				System.out.println("click");
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				accept.setBorder(BorderFactory.createRaisedBevelBorder());
				
			}

			@Override
			public void mouseExited(MouseEvent e) {
				accept.setBorder(BorderFactory.createEmptyBorder());
				
			}

			@Override
			public void mousePressed(MouseEvent e) {
				accept.setBorder(BorderFactory.createLoweredBevelBorder());
				
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				if(accept.contains(e.getPoint()))
					accept.setBorder(BorderFactory.createRaisedBevelBorder());
				else
					accept.setBorder(BorderFactory.createEmptyBorder());	
				
			}
			
			
			
			
		});
		JLabel decline = new JLabel("拒绝");
		decline.setForeground(new Color(215,0,0));
		decline.setBorder(BorderFactory.createEmptyBorder());
		decline.setOpaque(false);
		decline.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseClicked(e);
			}
			
			
			
		});
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER,15,15));
		buttonPanel.add(accept);
		buttonPanel.add(decline);
		
		this.add(label,"North");
		this.add(messagePanel,"Center");
		this.add(buttonPanel,"South");
		this.setBackground(new Color(250, 249, 242));
	}
	public IandAPanel(final String fromJID,final String toJID,final String crowdName,final String ns){
		
		System.out.println("creating panel....");
		try{
		this.ns = ns;
		this.crowdName = crowdName;
		String fromNick = Param.getNickNameFromJID(fromJID);
		
		String toNick = Param.getNickNameFromJID(toJID);
		title = "收到新消息";
		if(ns.equals(Param.invite_NS)){	
			
			message = fromNick + "邀请你加入群: \"" + crowdName + "\"";
		}else if(ns.equals(Param.apply_NS)){
			
			message = fromNick + "申请加入群 : \"" + crowdName + "\"";
		}
		setLayout(new BorderLayout());
		
		label = new JLabel(title);
		JLabel messageLabel = new JLabel();
		messageLabel.setText(message);
		messageLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		messagePanel = new JPanel();
		messagePanel.setOpaque(false);
		messagePanel.add(messageLabel);
		final JLabel accept = new JLabel("同意");
		accept.setForeground(new Color(0,245,0));
		accept.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
		accept.setOpaque(false);
		accept.addMouseListener(new MouseAdapter(){
			
			@Override
			public void mouseEntered(MouseEvent e) {
				accept.setBorder(BorderFactory.createRaisedBevelBorder());
				
			}

			@Override
			public void mouseExited(MouseEvent e) {
				accept.setBorder(BorderFactory.createEmptyBorder());
				
			}

			@Override
			public void mousePressed(MouseEvent e) {
				accept.setBorder(BorderFactory.createLoweredBevelBorder());
				
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				if(accept.contains(e.getPoint()))
					accept.setBorder(BorderFactory.createRaisedBevelBorder());
				else
					accept.setBorder(BorderFactory.createEmptyBorder());	
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				setVisible(false);
				UIEventManager.getInstance().sendResult(crowdName, fromJID,"agree",ns);
				removeSelf();
			}
	
		});
		final JLabel decline = new JLabel("拒绝");
		decline.setForeground(new Color(245,0,0));
		decline.setBorder(BorderFactory.createLineBorder(Color.black, 1));
		decline.setOpaque(false);
		decline.addMouseListener(new MouseAdapter(){
			
			@Override
			public void mouseEntered(MouseEvent e) {
				decline.setBorder(BorderFactory.createRaisedBevelBorder());
				
			}

			@Override
			public void mouseExited(MouseEvent e) {
				decline.setBorder(BorderFactory.createEmptyBorder());
				
			}

			@Override
			public void mousePressed(MouseEvent e) {
				decline.setBorder(BorderFactory.createLoweredBevelBorder());
				
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				if(accept.contains(e.getPoint()))
					decline.setBorder(BorderFactory.createRaisedBevelBorder());
				else
					decline.setBorder(BorderFactory.createEmptyBorder());	
				
			}
			
			
			@Override
			public void mouseClicked(MouseEvent e) {
				setVisible(false);
				
				UIEventManager.getInstance().sendResult(crowdName,fromJID, "reject",ns);
				removeSelf();
				
			}
			
		});
		buttonPanel = new JPanel();
		buttonPanel.setOpaque(false);
		buttonPanel.add(accept);
		buttonPanel.add(decline);
		System.out.println("last third");
		this.add(label,"North");
		this.add(messagePanel,"Center");
		this.add(buttonPanel,"South");
		
		this.setBackground(new Color(250, 249, 242));
		System.out.println("over...");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	private void removeSelf(){
		Container p = this.getParent();
		
		p.remove(this);
		p.validate();
		
		
	}
	
	public static void main(String[] args){
		IandAPanel ianda = new IandAPanel();
		JFrame jf = new JFrame();
		
		jf.setContentPane(ianda);
		jf.setSize(300, 280);
		jf.setVisible(true);
	}
}
